//
//  ViewController.m
//  百度地图
//
//  Created by ljm on 2017/7/5.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ViewController.h"
//#import "BMKClusterManager.h"

@interface ViewController ()<BMKMapViewDelegate,BMKRadarManagerDelegate,BMKPoiSearchDelegate>
{
    BMKMapView *_mapView;
    
    
    BMKPoiSearch *_searchr ;
    
    BMKPoiSearch *_poisearch;
    
    NSMutableArray *dataArr;
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self loadBMKMap];
    
//    [self setNearPlace];
    
//    [self sendWithPlace];
    
//    [self checkPlace];
    
//    [self searchPlace];
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth - 80, screenHeight - 40, 80, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"search" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchDetailPlace) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

#pragma mark -- 检索
- (void)searchPlace
{
    _searchr = [[BMKPoiSearch alloc] init];
    _searchr.delegate = self;
    
    BMKNearbySearchOption *option = [[ BMKNearbySearchOption alloc] init];
    /** 当前页索引 **/
    option.pageIndex = 1;
    /** 分页数量 **/
    option.pageCapacity = 10;
    option.location = CLLocationCoordinate2DMake(39.915, 116.404);
    option.keyword = @"小吃";
    BOOL flag = [_searchr poiSearchNearBy:option];
    
    if (flag) {
        NSLog(@"周边检索成功了");
    }else
    {
        NSLog(@"周边检索再次失败");
    }
}

#pragma mark -- 实现poisearchdelegate处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        NSLog(@"检索结果%@",searcher);

    }else if (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        NSLog(@"起点位置信息有歧义，搜索结果为%@",poiResult.cityList);
    }else{
        NSLog(@"未找到相关结果");
    }
}


#pragma mark -- poi的详细检索
- (void) searchDetailPlace
{
    _poisearch = [[BMKPoiSearch alloc] init];
    _poisearch.delegate = self;
    
    BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init];
    option.poiUid = @"昆明";
    BOOL flag = [_poisearch poiDetailSearch:option];
    if (flag) {
        NSLog(@"详细检索成功");
    }else
    {
        NSLog(@"详细检索失败");
    }
}

- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode
{
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        NSLog(@"%@",searcher);
        NSLog(@"详细检索结果%@",poiDetailResult);
    }else
    {
        NSLog(@"详细检索代码%u",errorCode);
    }
}

#pragma mark -- 加载百度地图
- (void)loadBMKMap
{
    self.title = @"百度地图";
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    
    /**
     *    设置地图为空白类型
     
     BMKMapTypeNone       = 0,               ///< 空白地图
     BMKMapTypeStandard   = 1,               ///< 标准地图
     BMKMapTypeSatellite  = 2,               ///< 卫星地图
     
     */
    //    _mapView.mapType = BMKMapTypeSatellite;
    
    /** 或
     设置为标准地图
     **/
    [_mapView setMapType:BMKMapTypeStandard];
    
    
    /** 打开实时路况图层,卫星和标准地图都可以打开 **/
    [_mapView setTrafficEnabled:YES];
    
    
    /** 打开百度城市热力图图层，百度自带数据 **/
    [_mapView setBaiduHeatMapEnabled:YES];
    
    
    
    
    //    self.view = _mapView;
    [self.view addSubview:_mapView];
}


#pragma mark -- 周边雷达
- (void) setNearPlace
{
    BMKRadarManager *_radarManager = [BMKRadarManager getRadarManagerInstance];
    
    /** 在不需要时，通过下边的方法使引用计数器减1 **/
    [BMKRadarManager releaseRadarManagerInstance];
    
    /** 上传和拉去位置信息，需要设置userid **/
    _radarManager.userId = @"baidu_ljm";
    
    /** 通过添加radar delegate获取自动上传时的位置信息，以及获得雷达操作结果 **/
    [_radarManager addRadarManagerDelegate:self];
    /** 在不需要时移除，否则会影响内存的释放 **/
//    [_radarManager removeRadarManagerDelegate:self];
    
    
}

#pragma mark -- 位置信息上传
- (void) sendWithPlace
{
    BMKRadarUploadInfo *myInfo = [[BMKRadarUploadInfo alloc] init];
    
    myInfo.extInfo = @"hello lihongkun";
    myInfo.pt = CLLocationCoordinate2DMake(39.916, 116.404);
    
    BMKRadarManager *_radarManager = [BMKRadarManager getRadarManagerInstance];
    BOOL res = [_radarManager uploadInfoRequest:myInfo];
    if (res) {
        NSLog(@"位置信息上传成功");
    }else
    {
        NSLog(@"位置信息上传失败");
    }
    
    
    /** 启用自动上传用户信息，需要实现getRadarAutoUpLoadInfo获取我的位置信息 **/
    [_radarManager startAutoUpload:5];
}


#pragma mark -- 发起检索请求
- (void) checkPlace
{
    BMKRadarNearbySearchOption *option = [[BMKRadarNearbySearchOption alloc] init];
    
    /** 搜索半径 **/
    option.radius = 8000;
    
    /** 排序方式 **/
    option.sortType = BMK_RADAR_SORT_TYPE_DISTANCE_FROM_FAR_TO_NEAR;
    
    option.centerPt = CLLocationCoordinate2DMake(39.916, 116.404);
    
    BMKRadarManager *_radarManager = [BMKRadarManager getRadarManagerInstance];
    [_radarManager addRadarManagerDelegate:self];
    
    BOOL res = [_radarManager getRadarNearbySearchRequest:option];
    if (res) {
        NSLog(@"位置检索成功");
    }else
    {
        NSLog(@"位置检索失败");
    }
}
#pragma mark -- 位置检索的相关代理方法
- (void)onGetRadarNearbySearchResult:(BMKRadarNearbyResult *)result error:(BMKRadarErrorCode)error
{
    NSLog(@"检索错误的：%u",error);
    if (error == BMK_RADAR_NO_ERROR) {
        NSLog(@"检索成功");
    }
}


#pragma mark -- 大头针，给地图添加标注
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    /** 添加一个大头针 **/
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"这里是北京";
    [_mapView addAnnotation:annotation];
    
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    static NSString *identifier = @"myAnnotation";
    
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        
        /**
         *    大头针的颜色
         
         BMKPinAnnotationColorRed = 0,
         BMKPinAnnotationColorGreen,
         BMKPinAnnotationColorPurple
         */
        newAnnotationView.pinColor = BMKPinAnnotationColorRed;
        
        
        /**
         *    设置改标注点动画显示
         */
        newAnnotationView.animatesDrop = YES;
        
        return newAnnotationView;
    }
    
    return nil;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_mapView viewWillDisappear];
    
    /**
     *    地图在不适用时候一定要置nil，否则会影响内存的释放
     */
    _mapView.delegate = nil;
    
    
    /**
     *    不使用时位置信息也要置空
     */
    _searchr.delegate = nil;
    
    _poisearch.delegate = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
